/**
 * Created by HUANGFR2 on 8/11/2016.
 */
(function ($) {
    "use strict";
    $(document).ready(function () {

        $("#menu-panel").mouseenter(function () {

            $('.menu-container').animate({width: '250px'});
            $(this).removeClass('menu-close');
            $('.operation-container').animate({'margin-left': '260px'});
        });
        $("#menu-panel").mouseleave(function () {
            $(this).addClass('menu-close');
            $('.menu-container').animate({width: '43px'});
            $('.operation-container').animate({'margin-left': '55px'});
        });
    });
}(jQuery));

function passAudit(maId){
    console.log(maId);
    updateStatus(maId, 1, function (sign) {
        var sign_ = JSON.parse(sign);
        if(sign_.msg == "SUCCESS"){
            var button = $('#merchant-'+maId).find('button');
            $(button[0]).parent().prev()[0].innerHTML = "Audit pass";
            button[0].disabled=true;
            $(button[0]).removeClass("btn-primary").addClass("btn-default");
            button[1].disabled=true;
            $(button[1]).removeClass("btn-warning").addClass("btn-default");
            button[2].disabled=false;
            $(button[2]).removeClass("btn-default").addClass("btn-danger");
        }else{
            alert(sign_.error);
        }
    })
}

function failedAudit(maId){
    updateStatus(maId, 2, function (sign) {
        var sign_ = JSON.parse(sign);
        if(sign_.msg == "SUCCESS"){
            var button = $('#merchant-'+maId).find('button');
            $(button[0]).parent().prev()[0].innerHTML = "Audit not pass";
            button[0].disabled=true;
            $(button[0]).removeClass("btn-primary").addClass("btn-default");
            button[1].disabled=true;
            $(button[1]).removeClass("btn-warning").addClass("btn-default");
        }else{
            alert(sign_.error);
        }
    })
}

function blockMerchant(event){
	var maId = event.data
    updateStatus(maId, 3, function (sign) {
        var sign_ = JSON.parse(sign);
        if(sign_.msg == "SUCCESS"){
            var button = $('#merchant-'+maId).find('button');
            $(button[0]).parent().prev()[0].innerHTML = "Blocked";
            button[0].disabled=false;
            $(button[0]).removeClass("btn-default").addClass("btn-primary");
            button[1].disabled=true;
            $(button[1]).removeClass("btn-warning").addClass("btn-default");
            button[2].disabled=true;
            $(button[2]).removeClass("btn-danger").addClass("btn-default");
        }else{
            alert(sign_.error);
        }
    })
}

function updateStatus(maId, status, cb){
    $.ajax({
            url: "../merchant/audit/"+maId+"/"+status,
            success: cb
        })
}

function blockLowGrade(event){
	var mId = event.data
	$.ajax({
		url: "../merchant/lowGrade/audit/"+ mId +"/3",
		success : function(sign){
			var sign_ = JSON.parse(sign);
	        if(sign_.msg == "SUCCESS"){
	            $('#lowGrade-'+mId).remove();
	        }else{
	            alert(sign_.error);
	        }
		}
	});
}

function updateDishStatus(daId, status, cb){
    $.ajax({
            url: "../dish/audit/"+daId+"/"+status,
            success: cb
        })
}

function failedDish(event){
	var daId = event.data;
    updateDishStatus(daId, 2, function(sign){
        var sign_ = JSON.parse(sign);
        if(sign_.msg == "SUCCESS"){
        	var button = $('#dish-'+daId).find('button');
        	button[0].disabled=true;
            $(button[0]).removeClass("btn-primary").addClass("btn-default");
            button[1].disabled=true;
            $(button[1]).removeClass("btn-warning").addClass("btn-default");
        }else{
            alert(sign_.error);
        }
    });
}

function passDish(daId){
    updateDishStatus(daId, 1, function(sign){
        var sign_ = JSON.parse(sign);
        if(sign_.msg == "SUCCESS"){
            var button = $('#dish-'+daId).find('button');
            button[0].disabled=true;
            $(button[0]).removeClass("btn-primary").addClass("btn-default");
        }else{
            alert(sign_.error);
        }
    });
}

function updateComplaintStatus(id, status){
	if(!status){
		var status = 3;
		id = id.data;
	}
	$.ajax({
        url: "../complaint/audit/"+id+"/"+status,
        success: function(sign){
        	var sign_ = JSON.parse(sign);
        	if(sign_.msg == "SUCCESS"){            		
        		disabledOperate(id);
        	}
        }
    })
	
}

function disabledOperate(id){
//	console.log("success");
	$('#complaint-'+id).find('.dropdown-toggle')[0].disabled = true;
}

function confirmBlock(id, type, mId){
    $('#confirmModel').modal('show');
    if(type == 'lm'){
        $('#confirm').click(id,blockLowGrade);
    }else if(type == 'am'){
        $('#confirm').click(id,blockMerchant);
    }else if(type == 'ad'){
        $('#confirm').click(id,failedDish);
    }else if(type == "oc"){
    	$('#confirm').click(id,updateComplaintStatus);
    }
}

function showDetail(id){
	if($('#detail-'+id)[0].style.display == "none"){
		$('#detail-'+id).fadeIn(500);
		$('#complaint-'+id).find('.glyphicon-plus').removeClass('glyphicon glyphicon-plus').addClass('glyphicon glyphicon-minus')
	}else{
		$('#detail-'+id).fadeOut(500);
		$('#complaint-'+id).find('.glyphicon-minus').removeClass('glyphicon glyphicon-minus').addClass('glyphicon glyphicon-plus')
	}
}
